package munyorderweb;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import munyorderweb_DB.Change_Bean;
import munyorderweb_DB.DB_manager;

public class Order_list extends HttpServlet {

	String nom = "*";
	String sql;


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		order(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		order(req,resp);

	}

	protected void order(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Connection con = null;
		sql = "select * from orders left join customer "
				+ "on orders.customer_no = customer.customer_no "
				+ "left join menu on menu.menu_no = orders.menu_no "
				+ "where customer_total = '0' ";

		if(req.getParameter("nom") != null){
			String no = req.getParameter("nom");
			sql += "and table_no = " + no;
		}
		try{
			//DB接続　SQL結果取得
			con = DB_manager.getConnection();
			Statement stm = con.createStatement();
			ResultSet rs = stm.executeQuery(sql);

			req.setCharacterEncoding("utf-8");
			resp.setContentType("text/html; charset=utf-8");

			//Change_Bean型でArrayListに投入
			Change_Bean change = null;
			ArrayList<Change_Bean> list = new ArrayList<Change_Bean>();
			while(rs.next()){
				change = new Change_Bean();
				change.setOrder_no(rs.getInt("order_no"));
				change.setOrder_count(rs.getInt("order_count"));
				change.setOrder_delivery(rs.getInt("order_delivery"));
				change.setTable_no(rs.getInt("table_no"));
				change.setMenu_name(rs.getString("menu_name"));
				list.add(change);
			}

			//表示用Jspファイルを呼出して画面上に表示
			req.setAttribute("list", list);
			RequestDispatcher dispatcher = req.getRequestDispatcher("Result_Order_list.jsp");
			dispatcher.include(req, resp);

		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				if(con != null){
					con.close();
				}
			}catch (SQLException e){
				e.printStackTrace();
			}
		}
	}
}
